//
//  RegularViewController.m
//  DirectMember
//
//  Created by jzd on 2018/6/20.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "RegularViewController.h"
#import "RegularCell.h"
#import "extractCell.h"
#import "RegularBuyController.h"

@interface RegularViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableV;
@property (weak, nonatomic) UISegmentedControl *segment;
@end

@implementation RegularViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"定期";
    [self setTitleView];
    
    [self.tableV registerNib:[UINib nibWithNibName:NSStringFromClass([RegularCell class]) bundle:nil] forCellReuseIdentifier:RegularCellIdentifier];
    [self.tableV registerNib:[UINib nibWithNibName:NSStringFromClass([extractCell class]) bundle:nil] forCellReuseIdentifier:extractCellIdentifier];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"buy"] style:UIBarButtonItemStylePlain target:self action:@selector(buyOnClick)];
}
-(void)buyOnClick{
    RegularBuyController *vc = [[RegularBuyController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)setTitleView{
    // 1.设置titleView
    UISegmentedControl *segMentControl = [[UISegmentedControl alloc] initWithItems:@[@"理财中",@"已提取"]];
    
    
//    UIImage *select_Image = [[UIImage imageNamed:@"white"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 0.1, 0.1, 0.1)];
    
    // 2.设置背景图片
    // 普通状态
//    [segMentControl setBackgroundImage:[UIImage imageNamed:@"remeber_Select"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    // 选中状态
//    [segMentControl setBackgroundImage:[UIImage imageNamed:@"whiteAA"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    
    // 3.设置字体颜色
    [segMentControl setTitleTextAttributes:@{
                                             NSForegroundColorAttributeName : [UIColor whiteColor]
                                             } forState:UIControlStateNormal];
    [segMentControl setTitleTextAttributes:@{
                                             NSForegroundColorAttributeName : TITLECOLOR
                                             } forState:UIControlStateSelected];
    
    // 4.设置选中的索引
    segMentControl.selectedSegmentIndex = 0;
    
    // 5.设置前景色
    segMentControl.tintColor = [UIColor whiteColor];
    
    [segMentControl addTarget:self action:@selector(segmentValueChange:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = segMentControl;
    self.segment = segMentControl;
}
-(void)segmentValueChange:(UISegmentedControl *)seg{
    [self.tableV reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.segment.selectedSegmentIndex == 0) {
        return 7;
    }else{
        return 3;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.segment.selectedSegmentIndex == 0) {
        RegularCell *cell = [tableView dequeueReusableCellWithIdentifier:RegularCellIdentifier];
        return cell;
    }else{
        extractCell *cell = [tableView dequeueReusableCellWithIdentifier:extractCellIdentifier];
        return cell;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.segment.selectedSegmentIndex == 0) {
        return 44;
    }else{
        return 80;
    }
}



@end
